package com.gem.growingtree.fragment;

import java.util.ArrayList;

import com.gem.growingtree.activity.R;

import android.content.res.Resources;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.View;
import android.view.Window;

import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends FragmentActivity {
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;
    private TextView  growthUp, parentTask, communication,babyLive;
    private ImageView ivGrowthUp,ivParentTask, ivCommunication,ivBabyLive;
    

    private int currIndex = 0;
    private Resources resources;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main1);
        
        resources = getResources();
        InitTextView();
        InitViewPager();
    }
    private void InitTextView() {
    	//�ײ��ĸ�   parentTask, communication,babyLive;
    	growthUp = (TextView) findViewById(R.id.tv_baby_growing_up);
    	parentTask = (TextView) findViewById(R.id.tv_parent_child_task);
    	communication = (TextView) findViewById(R.id.tv_communication_circle);
    	babyLive = (TextView) findViewById(R.id.tv_baby_live);
        
        
        ivGrowthUp=(ImageView) findViewById(R.id.iv_baby_growing_up);
        ivParentTask=(ImageView) findViewById(R.id.iv_parent_child_task);
        ivCommunication=(ImageView) findViewById(R.id.iv_communication_circle);
        ivBabyLive=(ImageView) findViewById(R.id.iv_baby_live);      
       // tvTitle.setText(R.string.tab_1);
        ivGrowthUp.setImageResource(R.drawable.baby_growing_up_green);
        
        ivGrowthUp.setOnClickListener(new MyOnClickListener(0));
        ivParentTask.setOnClickListener(new MyOnClickListener(1));
        ivCommunication.setOnClickListener(new MyOnClickListener(2));
        ivBabyLive.setOnClickListener(new MyOnClickListener(3));
    }
    
    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
        
        fragmentsList = new ArrayList<Fragment>();
        fragmentsList.add(new Fragment1());
        fragmentsList.add(new Fragment2());
        fragmentsList.add(new CommunicationCicle());
        fragmentsList.add(new Fragment4());
        
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
        mPager.setCurrentItem(0);
        growthUp.setTextColor(resources.getColor(R.color.title));

    }
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };
    
    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
            case 0:
                if (currIndex == 1) {
                    parentTask.setTextColor(resources.getColor(R.color.default_white));
                   ivParentTask.setImageResource(R.drawable.parent_child_task);
                  //  tvTitle.setText(R.string.tab_2);
                } else if (currIndex == 2) {
                    communication.setTextColor(resources.getColor(R.color.default_white));
                    ivCommunication.setImageResource(R.drawable.communication_circle);
                  //  tvTitle.setText(R.string.tab_3);
                } else if (currIndex == 3) {
                    babyLive.setTextColor(resources.getColor(R.color.default_white));
                    ivBabyLive.setImageResource(R.drawable.baby_live);
                   // tvTitle.setText(R.string.tab_4);
                }
                growthUp.setTextColor(resources.getColor(R.color.title));
                ivGrowthUp.setImageResource(R.drawable.baby_growing_up_green);
                //tvTitle.setText(R.string.tab_1);
                break;
            case 1:
                if (currIndex == 0) {
                    growthUp.setTextColor(resources.getColor(R.color.default_white));
                    ivGrowthUp.setImageResource(R.drawable.baby_growing_up);
                  //  tvTitle.setText(R.string.tab_1);
                } else if (currIndex == 2) {
                    communication.setTextColor(resources.getColor(R.color.default_white));
                   ivCommunication.setImageResource(R.drawable.communication_circle);
                  //  tvTitle.setText(R.string.tab_3);
                } else if (currIndex == 3) {
                    babyLive.setTextColor(resources.getColor(R.color.default_white));
                   ivBabyLive.setImageResource(R.drawable.baby_live);
                   // tvTitle.setText(R.string.tab_4);
                }
                parentTask.setTextColor(resources.getColor(R.color.title));
                ivParentTask.setImageResource(R.drawable.parent_child_task_green);
                //tvTitle.setText(R.string.tab_2);
                break;
            case 2:
                if (currIndex == 0) {
                  //  tvTabActivity.setTextColor(resources.getColor(R.color.default_white));
                    growthUp.setTextColor(resources.getColor(R.color.default_white));
                   ivGrowthUp.setImageResource(R.drawable.baby_growing_up);
                  //  tvTitle.setText(R.string.tab_1);
                } else if (currIndex == 1) {
                    parentTask.setTextColor(resources.getColor(R.color.default_white));
                    ivParentTask.setImageResource(R.drawable.parent_child_task);        
                   // tvTitle.setText(R.string.tab_2);
                } else if (currIndex == 3) {
                    babyLive.setTextColor(resources.getColor(R.color.default_white));
                    ivBabyLive.setImageResource(R.drawable.baby_live);
                  //  tvTitle.setText(R.string.tab_4);
                }
                communication.setTextColor(resources.getColor(R.color.title));
                ivCommunication.setImageResource(R.drawable.communication_circle_green);
                //tvTitle.setText(R.string.tab_3);
                break;
            case 3:
                if (currIndex == 0) {
                   
                    growthUp.setTextColor(resources.getColor(R.color.default_white));
                    ivGrowthUp.setImageResource(R.drawable.baby_growing_up);
                   // tvTitle.setText(R.string.tab_1);
                } else if (currIndex == 1) {
                    parentTask.setTextColor(resources.getColor(R.color.default_white));
                    ivParentTask.setImageResource(R.drawable.parent_child_task);        
                   // tvTitle.setText(R.string.tab_2);
                } else if (currIndex == 2) {
                    communication.setTextColor(resources.getColor(R.color.default_white));
                    ivCommunication.setImageResource(R.drawable.communication_circle);

                  //  tvTitle.setText(R.string.tab_3);
                }
                babyLive.setTextColor(resources.getColor(R.color.title));
                ivBabyLive.setImageResource(R.drawable.baby_live_green);

                //tvTitle.setText(R.string.tab_4);
                break;
            }
            currIndex = arg0;
            //������ֹʱͣ�������һ֡
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}